﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace LotteCinema.Admin
{
    public partial class QuanLySuatChieu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
                LaydSC();
        }
        public void LaydSC()
        {
            DataTable ds_sc = Timkiem_MASC("");
            string s;
            string str;
            
            for (int i = 0; i < ds_sc.Rows.Count; i++)
            {
                str = ds_sc.Rows[i]["MaSC"].ToString();
                DDLMaSC.Items.Add(str);
            }

            ds_sc = Timkiem_MACC("");
            for (int i = 0; i < ds_sc.Rows.Count; i++)
            {
                str = ds_sc.Rows[i]["MaCC"].ToString();
                DDLMaCC.Items.Add(str);
            }
            s = "select * from PHIM"; //bANG PHIM CUA M, SUA LAI DUM T
            ds_sc = DocBang(s);
            for (int j = 0; j < ds_sc.Rows.Count; j++)
            {
                str = ds_sc.Rows[j]["MaPhim"].ToString();
                DDLMaPhim.Items.Add(str);
            }
            ds_sc = Timkiem_MASC("");
            grSuatChieu.DataSource = ds_sc;
            grSuatChieu.DataBind();
            
        }

        static string strCnn = ConfigurationManager.ConnectionStrings["QuanLyRapPhim1ConnectionString"].ConnectionString.ToString();
        public static DataTable DocBang(string lenhSQL)
        {
            using (SqlConnection cnn = new SqlConnection(strCnn))
            {
                SqlDataAdapter bodocghi = new SqlDataAdapter(lenhSQL, cnn);
                DataTable bang = new DataTable();
                bodocghi.Fill(bang);
                return bang;
            }
        }


        protected void DDLMaSC_SelectedIndexChanged(object sender, EventArgs e)
        {
           // string s = "select * from suatchieu where masc='" + DDLMaSC.SelectedItem.ToString() + "'";
            DataTable dt = Timkiem_MASC(DDLMaSC.SelectedItem.ToString());
            if (dt.Rows.Count != 0)
            {
                DDLMaCC.Text = dt.Rows[0][1].ToString();
                DDLMaPhim.Text = dt.Rows[0][2].ToString();
                txtTenPhim.Text = dt.Rows[0][3].ToString();
            }
        }

        protected void imThem_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@macc", DDLMaCC.Text.ToString()));
                sqlPara.Add(new SqlParameter("@maphim", DDLMaPhim.Text.ToString()));
                CDatastore.ThucHienStore("SP_THEM_SUATCHIEU", sqlPara);
                lblThongBao.Text = "Bạn đã thêm Suất Chiếu thành công";
                DDLMaPhim.Items.Clear();
                DDLMaCC.Items.Clear();
                DDLMaSC.Items.Clear();
                txtTenPhim.Text = "";
                LaydSC();
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void imXoa_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@masc", DDLMaSC.Text.ToString()));
                CDatastore.ThucHienStore("SP_DELETE_SUATCHIEU", sqlPara);
                lblThongBao.Text = "Bạn đã xóa Mã Suất Chiếu " + DDLMaSC.Text + " thành công";
                DDLMaSC.Items.Clear();
                DDLMaCC.Items.Clear();
                DDLMaSC.Items.Clear();
                txtTenPhim.Text = "";
                LaydSC();
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void imSua_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                List<SqlParameter> sqlPara = new List<SqlParameter>();
                sqlPara.Add(new SqlParameter("@masc", DDLMaSC.Text.ToString()));
                sqlPara.Add(new SqlParameter("@macc", DDLMaCC.Text.ToString()));
                sqlPara.Add(new SqlParameter("@maphim", DDLMaPhim.Text.ToString()));
                CDatastore.ThucHienStore("SP_UPDATE_SUATCHIEU", sqlPara);
                lblThongBao.Text = "Bạn đã cập nhật Mã Suất Chiếu " + DDLMaSC.Text + " thành công";
                DDLMaSC.Items.Clear();
                
                DDLMaCC.Items.Clear();
                DDLMaSC.Items.Clear();
                txtTenPhim.Text = "";
                LaydSC();
            }
            catch (Exception ex)
            {
                lblThongBao.Text = ex.Message;
            }
        }

        protected void grSuatChieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            DDLMaSC.Text = grSuatChieu.SelectedRow.Cells[1].Text;
            DDLMaCC.Text = grSuatChieu.SelectedRow.Cells[2].Text;
            DDLMaPhim.Text = grSuatChieu.SelectedRow.Cells[3].Text;
            DataTable dt = Timkiem_MASC(DDLMaSC.Text);
            txtTenPhim.Text = dt.Rows[0]["tenphim"].ToString();

        }
        public DataTable Timkiem_MASC(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@MAsc", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_SUATCHIEU_MASC", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_SUATCHIEU_MASC");
        }
        public DataTable Timkiem_MACC(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@macc", ten));
                    return CDatastore.DocStore("SP_TIMKIEM_CACHIEU_MACC", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_TIMKIEM_CACHIEU_MACC");
        }


        public DataTable Timkiem_Ma_theotenphim(string ten)
        {
            try
            {
                if (ten != null || ten != "")
                {
                    List<SqlParameter> sqlPara = new List<SqlParameter>();
                    sqlPara.Add(new SqlParameter("@tenphim", ten));
                    return CDatastore.DocStore("SP_LIETKE_SUATCHIEU_TENPHIM", sqlPara);
                }
            }
            catch (Exception e)
            {
                lblThongBao.Text = e.Message;
            }
            return CDatastore.DocStore("SP_LIETKE_SUATCHIEU_TENPHIM");
        }
        protected void grSuatChieu_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grSuatChieu.PageIndex = e.NewPageIndex;
            grSuatChieu.DataBind();
            LaydSC();
        }

        protected void btTimKiem_Click(object sender, EventArgs e)
        {
            DataTable dt = Timkiem_Ma_theotenphim(txtTenPhim.Text);
            grSuatChieu.DataSource = dt;
            grSuatChieu.DataBind();
        }

    }
}